      SUBROUTINE BMAT(HW,M,BEGVL,FNORM,B,MX,DEPW,RHOW,GRHOW,NPW,LHW,
     1                DEPB,RHOB,GRHOB,NPB,LHB)
C
C     THE FOLLOWING SUBROUTINE COMPUTES THE POSITIVE DEFINITE 
C     SYMETRIC MATRIX B NEEDED BY GALRKN. THIS MATRIX IS USED 
C     TO FORM PART OF THE GENERALIZED PROBLEM THAT IS SOLVED TO
C     FIND THE EIGENVALUES AND EIGENFUNCTIONS IN TERMS OF THE BASIC
C     EIGENFUNCTIONS.
C
      IMPLICIT REAL*8 (A-H,O-Z)
C
      DIMENSION DEPW(MX),RHOW(MX),GRHOW(MX)
      DIMENSION DEPB(MX),RHOB(MX),GRHOB(MX)
      REAL*8 B(MX,MX)
      COMPLEX*16 BEGVL(MX)
      COMPLEX*16 ONE
      COMPLEX*16 CDSRT,CEXIB,CEXIW
      COMPLEX*16 ALPHA1,ALPHA2,BETA1,BETA2
      COMPLEX*16 FNORM(MX)
C
      COMMON /BLKEVN/ HB,CW,CB,FKW,FKB,ROHW,ROHB,ATEN
C
      ONE=DCMPLX(1.0,0.0)
      LHWM1=LHW-1
      LHBM1=LHB-1
C
C     COMPUTE THE (REAL) SYMMETRIC MATRIX B 
C
      DO 500 I=1,M
      ALPHA1=CDSRT(FKW**2+FKB**2*(BEGVL(I)-ONE))
      BETA1=CDSRT(FKB**2*BEGVL(I))
      DO 400 II=I,M
      ALPHA2=CDSRT(FKW**2+FKB**2*(BEGVL(II)-ONE))
      BETA2=CDSRT(FKB**2*BEGVL(II))
C
C     INITIALIZE WITH ZERO
C
      B(II,I)=0.0D0
C
C     SUM OVER WATER LAYERS
C
      IF(LHWM1 .EQ. 0) GO TO 200
      DO 150 L=1,LHWM1
      Z1=DEPW(L)
      Z2=DEPW(L+1)
      B(II,I)=B(II,I)+DREAL(CEXIW(Z1,Z2,DEPW(L),RHOW(L),
     1                      GRHOW(L),ALPHA1,ALPHA2))
  150 CONTINUE
  200 CONTINUE
C
      Z1=DEPW(LHW)
      B(II,I)=B(II,I)+DREAL(CEXIW(Z1,HW,DEPW(LHW),RHOW(LHW),
     1                      GRHOW(LHW),ALPHA1,ALPHA2))
C
C     SUM OVER BOTTOM LAYERS
C
      IF(LHBM1 .EQ. 0) GO TO 300
      DO 250 L=1,LHBM1
      Z1=DEPB(L)
      Z2=DEPB(L+1)
      B(II,I)=B(II,I)+DREAL(CEXIB(HW,HW,Z1,Z2,DEPB(L),RHOB(L),
     1                      GRHOB(L),ALPHA1,ALPHA2,BETA1,BETA2))
  250 CONTINUE
  300 CONTINUE
C
      Z1=DEPB(LHB)
      B(II,I)=B(II,I)+DREAL(CEXIB(HW,HW,Z1,HB,DEPB(LHB),RHOB(LHB),
     1                      GRHOB(LHB),ALPHA1,ALPHA2,BETA1,BETA2))
C
C     APPLY NORMALIZATION FOR THE BASIC EIGENFUNCTIONS
C
      B(II,I)=B(II,I)/DREAL(CDSRT(FNORM(II))*CDSRT(FNORM(I)))
C
C     DEFINE THE SUB DIAGONAL ENTRIES BY SYMMETRY
C
      IF(II .NE. I) B(I,II)=B(II,I)
C
  400 CONTINUE
  500 CONTINUE
C
      RETURN
      END
